<?
		
	function get_groups($filter=array('g:500'), $negative=true, $users=false){
		$like = ($users) ? false : " AND `nam` NOT LIKE ':%'";
		return get_filtered('groups', "id, nam", $filter, $negative, $like);
	}
	
	function get_users($filter=array('u:500'), $negative=true){
		return get_filtered('users', "id, name, email, class", $filter, $negative);
	}
	
	function get_doms($filter=false, $negative=true){
		return get_filtered('domains', "id, name, class", $filter, $negative);
	}
	
	function get_filtered($table, $cols, $filter, $negative, $extra=''){
		$qs = "SELECT $cols FROM $table WHERE ";
		if(!is_array($filter)){
			 $qs .= '1 ';
		}elseif($negative){
			if(count($filter)){
				foreach((array) $filter as $gid) if($gid) $qs .= "id != '$gid' AND ";
				$qs = substr($qs, 0, -5);
			}else{
				$qs .= '1 ';
			}
		}else{
			if(!count($filter)) return array();
			foreach((array) $filter as $gid) if($gid) $qs .= "id = '$gid' OR ";
			$qs = substr($qs, 0, -4);
		}
		if($extra) $qs .= $extra;
		$qs .= ';';
		$al = smdb()->open();
		$q = $al->query($qs);
		if((!$q)||(mysqli_num_rows($q) == 0)) return array();
		while($row = mysqli_fetch_array($q, MYSQLI_ASSOC)) $results[$row['id']] = $row;
		$q->close();
		return $results;
	}

	function mkGroupJsObj($u){
		$o = new obj;
		$u = new obj($u);
		$o->name = (substr($u->nam, 0, 1) == ':') ? smdb(substr($u->nam, 1))->name : $u->nam;
		$o->id = $u->id;
		$o->rel = substr($u->id, 2);
		$o->title = $o->name;
		$o->icon = 'Group.png';
		$o->modpath = 'SmRoot/0v0r01';
		return $o;
	}

	function serializeGroups($groups){
		$o = new json;
		foreach($groups as $id=>$u) $o->$id = mkGroupJsObj($u);
		return $o;
	}
	
	function mkUserJsObj($u){
		$o = new obj;
		$u = new obj($u);
		$o->name = $u->name;
		$o->id = $u->id;
		$o->rel = substr($u->id, 2);
		$o->title = $u->name . ' : ' . $u->class . ', ' . $u->email;
		$o->icon = $u->class.'.png';
		$o->modpath = 'SmRoot/0v0r01';
		return $o;
	}

	function serializeUsers($users){
		$o = new json;
		foreach($users as $id=>$u) $o->$id = mkUserJsObj($u);
		return $o;
	}
	
	function mkDomJsObj($u, $id=false){
		$o = new obj;
		$u = new obj($u);
		$o->name = $u->name;
		$o->id = ($id) ? $id : substr($u->id, 2);
		$o->rel = ($id) ? $id : substr($u->id, 2);
		$o->title = $u->name;
		$o->icon = $u->class.'.png';
		$o->modpath = 'SmRoot/0v0r01';
		return $o;
	}

	function serializeDoms($doms){
		$o = new json;
		foreach($doms as $id=>$u){
			$id = substr($id, 2);
			$o->$id = mkDomJsObj($u, $id);
		}
		return $o;
	}
	
	function GUIdomData($dom){
		$domobj = $dom->dbObj();
		return new obj(array(
			'id'=>$dom->DOM_ID,
			'own'=>$domobj->own,
			'grp'=>$domobj->grp,
			'ini'=>(string)$dom->get_config(),
			'name'=>$dom->NAME,
			'class'=>$domobj->class,
		));
	}
?>